/**
 * @package Autogenerator for Joomla!
 * @version 3.0.0
 * @author Truong Cong Duc <truongcongduc4@gmail.com>
 * @link autogenerator.blogspot.com
 * @copyright (c) 2013, Truong Cong Duc
 * @license GNU General Public License version 2 or later; see LICENSE.txt
 */

var Autogenerator = {};

Autogenerator.Import = {
    checkAll: function(){
        var aListCheckBox = jQuery('.list-tables-import').find('input[type="checkbox"]');
        
        aListCheckBox.each(function(i){
            var oCheckbox = jQuery(this);
            
            var bCurrentChecked = oCheckbox.prop('checked');
            
            oCheckbox.prop('checked', !bCurrentChecked);
        });
    },
    clearAll: function(){
        var aListCheckBox = jQuery('.list-tables-import').find('input[type="checkbox"]');
        
        aListCheckBox.each(function(i){
            jQuery(this).prop('checked', false);
        });
    }
};

Autogenerator.XML_Element = {
    checkAll: function(){
        var aListCheckBox = jQuery('.list-xml-attribute-list').find('input[type="checkbox"]');
        
        aListCheckBox.each(function(i){
            var oCheckbox = jQuery(this);
            
            var bCurrentChecked = oCheckbox.prop('checked');
            
            oCheckbox.prop('checked', !bCurrentChecked);
        });
    },
    clearAll: function(){
        var aListCheckBox = jQuery('.list-xml-attribute-list').find('input[type="checkbox"]');
        
        aListCheckBox.each(function(i){
            jQuery(this).prop('checked', false);
        });
    }
};

Autogenerator.Edit_Inline = {
    init: function(){
        var t = this;
	
        var aElements = jQuery('.autogenerator-edit-inline');
        
        if (aElements)
        {
            aElements.each(function(i){
                var oElement = jQuery(this);
                
                oElement.click(function(){
                    // Hide the current element.
                    oElement.hide();
                    
                    // Get the place holder.
                    var oHolder = oElement.parent().find('.autogenerator-holder');
                    
                    // Show the holder.
                    oHolder.show();
					
                    var oCustom = oHolder.find('.autogenerator-custom');
					
                    // Set value for custom.
                    oCustom.val(jQuery.trim(oElement.text()));
                });
            });
        }
		
        t.save();
        t.cancel();
    },
    save: function(){
        var sLink = 'index.php?option=com_autogenerator&task=xml_element.editInline';
		
        var oLink = jQuery('.autogenerator-link');
        if (oLink)
        {
            sLink = oLink.attr('href');
        }
	
        var aSave = jQuery('.autogenerator-save');
		
        aSave.each(function(i){
            var oSave = jQuery(this);
			
            oSave.click(function(oEvent){
                oEvent.preventDefault();
				
                var oHolder = oSave.parent();
				
                var iId = oHolder.find('.autogenerator-id').val();
				
                var oCustom = oHolder.find('.autogenerator-custom');
				
                // Get the custom field value.
                var sCustom = jQuery.trim(oCustom.val());
				
                oCustom.val(sCustom);
				
                var oElement = oHolder.parent().find('.autogenerator-edit-inline');
				
                // Call ajax to save.
                jQuery.ajax({
                    url: sLink,
                    type: 'POST',
                    data: {
                        'data[id]': iId,
                        'data[custom]': sCustom
                    },
                    dataType: 'html',
                    success: function(sResult){
                        if (sResult == 1)
                        {
                            // Set the new custom value.
                            oElement.html(sCustom);
							
                            // Clear value of custom.
                            oCustom.val('');
							
                            // Hide the holder.
                            oHolder.hide();
							
                            // Show the element.
                            oElement.show();
                        }
                        else
                        {
                            alert(sResult);
                        }
                    }
                });
            });
        });
    },
    cancel: function(){
        var aCancel = jQuery('.autogenerator-cancel');
		
        aCancel.each(function(i){
            var oCancel = jQuery(this);
			
            oCancel.click(function(oEvent){
                oEvent.preventDefault();
				
                var oHolder = oCancel.parent();
				
                var oElement = oHolder.parent().find('.autogenerator-edit-inline'); 
				
                var oCustom = oHolder.find('.autogenerator-custom');
				
                // Clear value of custom.
                oCustom.val('');
				
                // Hide the holder.
                oHolder.hide();
				
                // Show the element.
                oElement.show();
            });
        });
    }
}

Autogenerator.Tables = {
    importFields: function(){
        var aImport = jQuery('.tables-import-fields');
		
        aImport.each(function(){
            var oImport = jQuery(this);
			
            oImport.click(function(oEvent){
                oEvent.preventDefault();
                if (confirm('Auto-import will delete all old fields and old MVC of this table. Do you want to continue ?'))
                {
                    // Call ajax to save.
                    jQuery.ajax({
                        url: oImport.attr('href'),
                        type: 'GET',
                        dataType: 'html',
                        success: function(iResult){
                            alert('Import successfully! Total : ' + iResult + ' field(s).');
                        }
                    });
                }
            });
        });
    }
}

Autogenerator.Change_Status = {
    init: function(){
        var aStatus = jQuery('.autogenerator-change-status');
		
        aStatus.each(function(){
            var oStatus = jQuery(this);
			
            oStatus.click(function(oEvent){
                oEvent.preventDefault();
				
                // Call ajax to save.
                jQuery.ajax({
                    url: oStatus.attr('href'),
                    type: 'POST',
                    data: {},
                    dataType: 'html',
                    success: function(sResult){
                        var oIcon = oStatus.find('i');
                        
                        if (oStatus.hasClass('autogenerator-change-unique'))
                        {
                            location.reload();
                            return;
                        }
                        
                        if (sResult == 1)
                        {
                            oIcon.attr('class', 'icon-publish');
                        }
                        else if (sResult == 0)
                        {
                            oIcon.attr('class', 'icon-unpublish');
                        }
                        else
                        {
                            alert(sResult);
                        }
                    }
                });
            });
        });
    }
}

Autogenerator.Change_Type = {
    init: function(){
        var aStatus = jQuery('.autogenerator-change-type');
		
        aStatus.each(function(){
            var oStatus = jQuery(this);
			
            oStatus.click(function(oEvent){
                oEvent.preventDefault();
				
                // Call ajax to save.
                jQuery.ajax({
                    url: oStatus.attr('href'),
                    type: 'POST',
                    data: {},
                    dataType: 'html',
                    success: function(sResult){
                        var oIcon = oStatus.find('i');
                        var oString = oStatus.find('.autogenerator-change-string');
                        if (oStatus.hasClass('autogenerator-change-unique'))
                        {
                            location.reload();
                            return;
                        }
                        
                        if (sResult == 1)
                        {
                            oIcon.attr('class', 'icon-list-view');
                            oString.html('List');
                        }
                        else if (sResult == 0)
                        {
                            oIcon.attr('class', 'icon-checkin');
                            oString.html('Item');
                        }
                        else
                        {
                            alert(sResult);
                        }
                    }
                });
            });
        });
    }
}

Autogenerator.Select_Inline = {
    init: function(){
        var t = this;
	
        var aSelectInlineTitle = jQuery('.autogenerator-select-inline-xml-type-title');
		
        aSelectInlineTitle.each(function(){
            var oSelectInlineTitle = jQuery(this);
			
            oSelectInlineTitle.click(function(oEvent){
                oEvent.preventDefault();
				
                // Hide itself.
                oSelectInlineTitle.hide();
				
                var oSelectHolder = oSelectInlineTitle.parent().find('.autogenerator-select-holder');
				
                oSelectHolder.show();
            });
        });
		
        t.save();
        t.cancel();
    },
	
    save: function(){
        var sLink = jQuery('.autogenerator-select-link').attr('href');
	
        var aSave = jQuery('.autogenerator-select-save');
		
        aSave.each(function(){
            var oSave = jQuery(this);
			
            oSave.click(function(oEvent){
                oEvent.preventDefault();
				
                var oSelectHolder = oSave.parent();
				
                var oSelectList = oSelectHolder.find('.autogenerator-select-list');
		
                // Get all options.
                var aAllOptions = oSelectList.find('option');
		
                // Clear selected.
                aAllOptions.each(function(){
                    var oOption = jQuery(this);
					
                    oOption.removeAttr('selected');
                });
				
                // Get all active results.
                var aAllResults = oSelectHolder.find('.active-result');
                
                // Assign value for the list.
                aAllResults.each(function(iIndex){
                    var oResult = jQuery(this);
					
                    if (oResult.hasClass('result-selected')) {
                        var sId = oResult.attr('id');
                        var aTemp = sId.split('_o_');
                        var iPos = 0;
                        
                        if (aTemp.length == 2)
                        {
                            iPos = aTemp[1];
                        }
                        
                        var oOption = jQuery(aAllOptions[iPos]);
						
                        oOption.attr('selected', 'selected');
                    }
                });
				
                // Get the XML type id.
                var iXmlTypeId = 0;
				
                if (oSelectList.val())
                    iXmlTypeId = oSelectList.val();
				
                if (iXmlTypeId == 0)
                {
                    alert('Please select the XML type!');
                    return false;
                }
				
                // Get field.
                var oField = oSelectHolder.find('.autogenerator-select-field-id');
				
                // Call ajax to save.
                jQuery.ajax({
                    url: sLink,
                    type: 'POST',
                    data: {
                        'data[id]': oField.val(),
                        'data[value]': iXmlTypeId
                    },
                    dataType: 'html',
                    success: function(sResult){
                        if (sResult == 1)
                        {
                            var oOption = oSelectList.find('option[selected^="selected"]');
							
                            var oSelectInlineTitle = oSelectHolder.parent().find('.autogenerator-select-inline-xml-type-title');
							
                            // Set new value.
                            oSelectInlineTitle.html(oOption.html());
							
                            oSelectInlineTitle.show();
							
                            oSelectHolder.hide();
                        }
                        else
                        {
                            alert(sResult);
                        }
                    }
                });
            });
        });
    },
	
    cancel: function(){
        var aCancel = jQuery('.autogenerator-select-cancel');
		
        aCancel.each(function(){
            var oCancel = jQuery(this);
			
            oCancel.click(function(oEvent){
                oEvent.preventDefault();
				
                var oSelectHolder = oCancel.parent();
				
                // Original value.
                var oXmlTypeId = oSelectHolder.find('.autogenerator-select-xml-type-id');
				
                var oSelectTitle = oSelectHolder.parent().find('.autogenerator-select-inline-xml-type-title');
				
                var oChoiseSingle = oSelectHolder.find('.chzn-single');
				
                var oSingleSpan = oChoiseSingle.find('span');
				
                // Get list of result.
                var oListResult = oSelectHolder.find('.chzn-results');
				
                var aResults = oListResult.find('li');
				
                var oSelectList = oSelectHolder.find('.autogenerator-select-list');
				
                // Reset value for list.
                oSelectList.val(oXmlTypeId.val());
				
                // Reset the 
				
                var oOption = oSelectList.find('option[selected^="selected"]');
				
                var sOption = 'Select an option';
				
                if (oOption.length > 0)
                    sOption = oOption.html();
				
                oSingleSpan.html(sOption);
				
                // Clear old value.
                aResults.each(function(){
                    var oResult = jQuery(this);
					
                    if (oResult.html() == sOption)
                    {
                        if (!oResult.hasClass('result-selected'))
                        {
                            oResult.addClass('result-selected');
                        }
                    }
                    else
                    {
                        if (oResult.hasClass('result-selected'))
                        {
                            oResult.removeClass('result-selected');
                        }
                    }
                });
				
                // Reset value.
				
                oSelectHolder.hide();
				
                oSelectTitle.show();
            });
        });
    }
}